# codeing = gbk

# @Time : 2022/9/6 13:45
# @Autgor : 影驰
# @File： HousePricesFlask.py
# @Software : PyCharm

from flask import Flask
from flask.views import MethodView
import pandas as pd
from flask_cors import CORS



app = Flask(__name__)
CORS().init_app(app)


def pdShow(area_name):
    print(area_name)
    if area_name=="全部地区平均":
    # if 1==1:
        data = pd.read_csv("RegionalHousePrice/全部地区平均.csv", encoding="gbk", header=None)
        data = data.rename(columns={0: "name", 1: "value"})
    else:
        data = pd.read_csv(f"RegionalHousePrice/{area_name}.csv", encoding="gbk", header=None)
        data = data.loc[:, [1, 2]]
        data = data.rename(columns={1: "name", 2: "value"})
    data = data.to_dict(orient="records")
    print(data)
    return data

@app.route("/")
def index():
    return "index"


class PricesApi(MethodView):
    def get(self,area_name):
        show_data = pdShow(area_name)
        return show_data

prices_view =PricesApi.as_view("prices_api")
app.add_url_rule("/prices",defaults={"area_name":"全部地区平均"}
                 ,view_func=prices_view,methods=['GET',])
app.add_url_rule("/prices/<area_name>",view_func=prices_view,methods=['GET',])


if __name__ == '__main__':
    app.run(debug=True)
